﻿using Volo.Abp;
using Volo.Abp.Application;
using Volo.Abp.AspNetCore.ExceptionHandling;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Serilog;
using Volo.Abp.Auditing;
using Volo.Abp.Autofac;
using Volo.Abp.AutoMapper;
using Volo.Abp.Domain;
using Volo.Abp.EntityFrameworkCore.MySQL;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.Modularity;
using Volo.Abp.Swashbuckle;
using Infortime.DbContexts;

namespace Infortime
{
    [DependsOn(typeof(AbpAspNetCoreMvcModule),
        typeof(AbpAutofacModule),
        typeof(AbpEntityFrameworkCoreModule),     //AbpEntityFrameworkCore模块
        typeof(AbpEntityFrameworkCoreMySQLModule),//AbpEntityFrameworkCoreMySQL模块
        typeof(AbpDddDomainModule),
        typeof(AbpDddApplicationModule),
        typeof(AbpAutoMapperModule),
        typeof(AbpSwashbuckleModule),
        typeof(AbpAspNetCoreSerilogModule)
        )]
    public class InfortimeModule : AbpModule
    {
        private const string DefaultCorsPolicyName = "Default";

        public override Task ConfigureServicesAsync(ServiceConfigurationContext context)
        {
            var configuration = context.Services.GetConfiguration();

            var host = context.Services.GetHostingEnvironment();

            var service = context.Services;

            //请求日志
            Configure<AbpAuditingOptions>(optios =>
            {
                //默认关闭，开启会有大量的审计日志
                optios.IsEnabled = true;
                //审计日志过滤器，符合条件的才记录
                optios.AlwaysLogSelectors.Add(x => Task.FromResult(x.Url is null ? true : !x.Url.StartsWith("/api/app/file/")));
            });

            //配置错误处理显示详情
            Configure<AbpExceptionHandlingOptions>(options =>
            {
                options.SendExceptionsDetailsToClients = true;
            });

            //配置自动API
            Configure<AbpAspNetCoreMvcOptions>(options =>
            {
                options.ConventionalControllers.Create(typeof(InfortimeModule).Assembly,
                    options => options.RemoteServiceName = "default");
                //统一前缀
                //options.ConventionalControllers.ConventionalControllerSettings.ForEach(x => x.RootPath = "ss/appss");
            });

            //注册DbContext上下文
            service.AddAbpDbContext<InfortimeDbContext>();

            //集成MySQL
            Configure<AbpDbContextOptions>(options =>
            {
                options.UseMySQL();
            });

            service.AddControllers();
            
            service.AddEndpointsApiExplorer();
            service.AddAbpSwaggerGen(options =>
            {
                options.DocInclusionPredicate((docName, api) => true);//显示自动API
            });

            return Task.CompletedTask;
        }

        public override Task OnApplicationInitializationAsync(ApplicationInitializationContext context)
        {
            IApplicationBuilder app = context.GetApplicationBuilder();
            IWebHostEnvironment env = context.GetEnvironment();
            // Configure the HTTP request pipeline.
            if (env.IsDevelopment())
            {
                app.UseSwagger();
                app.UseAbpSwaggerUI(options =>
                {
                    options.SwaggerEndpoint("/swagger/v1/swagger.json", "InfortimeAPI");
                });
            }

            //使用静态资源
            app.UseStaticFiles();

            app.UseHttpsRedirection();

            //配置abp路由
            app.UseRouting();

            app.UseAuthorization();

            //审计日志
            app.UseAuditing();

            //日志记录
            app.UseAbpSerilogEnrichers();

            app.UseConfiguredEndpoints();

            return Task.CompletedTask;
        }
    }
}
